package com.loriot.common.shifting_letters_ii_2381;

/**
 * @Title: Main
 * @Package: com.loriot.common.shifting_letters_ii_2381
 * @Description:
 * @Author: zzy boy
 * @Date: 2025/10/24 9:11
 * @Version: 0.0.1-alpha
 */

public class Main {
    class Solution {
        public String shiftingLetters(String s, int[][] shifts) {
            int[] differ = new int[s.length()+1];
            for(int[] item:shifts){
                int start = item[0];
                int end = item[1]+1;
                int opera = item[2];
                if(opera==0){
                    differ[start] += -1;
                    differ[end] += 1;
                }else{
                    differ[start] += 1;
                    differ[end] += -1;
                }
            }
            StringBuilder rst = new StringBuilder();
            int offset = 0;
            for(int i=0;i<differ.length-1;i++){
                offset+=differ[i];
                // int k = s.charAt(i)-'a'+(26+offset%26)%26; // 偏移量
                // char c = (char)('a'+k%26);
                int k = (s.charAt(i) - 'a' + offset) % 26;
                if (k < 0) k += 26;  // 保证非负
                char c = (char) ('a' + k);
                rst.append(c);
            }
            return rst.toString();

        }
    }
}
